package yja.gzw.app.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType
abstract class BaseFragment<VM: ViewModel,D: ViewDataBinding>(private var layoutid:Int): Fragment() {
    lateinit var viewmodel:VM
    lateinit var binding:D

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding= DataBindingUtil.inflate(inflater,layoutid,container,false)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val type=javaClass.genericSuperclass as ParameterizedType
        val clazz=type.actualTypeArguments[0] as Class<VM>
        viewmodel= ViewModelProvider(this)[clazz]
        initView()
        dataObservese()

    }

    abstract fun initView()

    abstract fun dataObservese()
    override fun onDestroy() {
        super.onDestroy()
        if(binding!=null)
        {
            binding.unbind()

        }

    }

}